<?php

/*
 * litchicms
 * Copyright (C) 2010-5-24 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

check_make_dir(BRK . $BRK_CONF['htmldir']);

$in_update = false;

if (!empty($_POST['updatehtml'])) {
    $in_update = true;
}
$temp = array_filter($RT['v']);
if ($temp['m'] == $temp['part']) {
    unset($temp['part']);
}
$filenameTemp = $temp['m'];
unset($temp['m']);
foreach ($temp as $k => $value) {
    $filenameTemp.='_' . $k . '_' . $value;
}
//$filenameTemp = substr($filenameTemp, 0, -1);
if ($filenameTemp == $BRK_SET['defualt_channel'] && $BRK_SET['defualt_channel'] == $RT['v']['m']) {
    $htmlname = 'index.html';
} else {
    $htmlname = $BRK_CONF['htmldir'] . '/' . $filenameTemp . '.html';
}

if ($in_update) {
    unlink(BRK . $htmlname);
}

//如果存在 
if (is_file(BRK . $htmlname)) {

//    if ($cache_info['cache_datetime'] + $BRK_SET['cachetime'] > $BRK_GLOBAL['datearray'][0]) {
//        require $cachefile . '.php';
//    }
}
//生成
else {
    ob_start();
    require 'cachemode/nocache.inc.php';
    if (empty($GLOBALS['ismsg'])) {
        $html_contents = ob_get_contents();

        url_replace_additem($BRK_GLOBAL['REQUEST_URI_OLD'], '/' . $htmlname);
        $html_contents = url_replace($html_contents);

        $infoarr = array(
            'cache_datetime' => $BRK_GLOBAL['datearray'][0],
            'htmltime' => $BRK_SET['htmltime'],
            'url' => base64_encode($BRK_GLOBAL['REQUEST_URI_OLD']),
        );

        $infostr = "\n<script language=\"javascript\" type=\"text/javascript\">AYCMS_tohtml_info=" . json_encode($infoarr) . "</script>\n<script language=\"javascript\" type=\"text/javascript\" src=\"" . $GLOBALS['BRK_CONF']['rootdir'] . '/system/utils/539247.js' . "\"></script>";

        file_put_contents(BRK . $htmlname, $html_contents . $infostr);
        ob_clean();
    }
}
!function_exists('openSQL') && require 'main.fun.php';
if (empty($Db)) {
    $Db = openSQL();
}

if (!empty($_SERVER['HTTP_REFERER'])) {
    $urlarr = parse_url($_SERVER['HTTP_REFERER']);
    if (is_file(BRK . $urlarr['path'])) {
        url_replace_additem($BRK_GLOBAL['REQUEST_URI_OLD'], '/' . $htmlname);
        $content = file_get_contents(BRK . $urlarr['path']);
        $content = url_replace($content);
        file_put_contents(BRK . $urlarr['path'], $content);
    }
}

function url_replace($content) {
    global $Db;
    static $ALL_url = array();
    if (empty($ALL_url)) {
        $rs = brk_select('url_replace');
        while ($row = $Db->fetch_array($rs)) {
            $ALL_url[] = $row;
        }
    }

    foreach ($ALL_url as $v) {
        $content = str_replace($v['url'] . "'", $v['url_replace'] . "'", $content);
        $content = str_replace($v['url'] . "\"", $v['url_replace'] . "\"", $content);
        $content = str_replace($v['url'] . " ", $v['url_replace'] . " ", $content);
    }

    return $content;
}

function url_replace_additem($from, $to) {
    if (!brk_select_one('url_replace', '*', "WHERE url='" . addslashes($from) . "'")) {
        brk_insert('url_replace', array('url' => addslashes($from), 'url_replace' => addslashes($GLOBALS['BRK_CONF']['rootdir'] . $to)));
    }
}

if ($in_update) {
    echo json_encode(array('isOK' => 1));
} else {
    header301("/" . $htmlname);
}